package ihooyah.com.whitehat

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Intent
import android.view.View
import ihooyah.com.whitehat.device.DeviceFeedBackListener
import ihooyah.com.whitehat.device.DeviceMethod
import ihooyah.com.whitehat.device.DeviceReceiver
import ihooyah.com.whitehat.utils.*
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.*


class MainActivity : AppCompatActivity(), DeviceFeedBackListener {

    private var deviceMethod: DeviceMethod? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }


    private fun initView() {
        //开启毁灭模式
        btClick.setOnClickListener { _ ->
            showSimpleAlert(this, "即将开启毁灭模式") {
                startDevice()
                confirmStatus()
            }
        }
        //关闭毁灭模式
        btClose.setOnClickListener { _ ->
            showSimpleAlert(this, "即将关闭毁灭模式") {
                getMethod().onRemoveActivate()
                confirmStatus()
            }
        }
        //功能列表
        btLock.setOnClickListener { _ ->
            val pairList = ArrayList<Pair<String, OnButtonClickListener>>()
            //添加子项目
            pairList.add(Pair("延时息屏", OnButtonClickListener {
                alertInput(this) {
                    getMethod().lockByTime(it)
                }
            }))
            pairList.add(Pair("直接息屏", OnButtonClickListener {
                getMethod().lockNow()
            }))
            pairList.add(Pair("恢复出厂设置", OnButtonClickListener {
                showSimpleAlert(this, "警告！恢复出厂设置将清空所有数据！") {
                    getMethod().wipeData()
                }
            }))
            pairList.add(Pair("禁用摄像头", OnButtonClickListener {
                showCameraDisabled(this, getMethod().cameraDisabled) { disabled ->
                    getMethod().disableCamera(disabled)
                }
            }))
            //展示list
            showButtonList(this, pairList)
        }
        //地图
        btMap.setOnClickListener {
            val intent = Intent(this@MainActivity, MapActivity::class.java)
            startActivity(intent)
        }
    }

    private fun startDevice() {
        val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
        val deviceComponentName = ComponentName(packageName, DeviceReceiver::class.java.name)
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceComponentName)
        this.startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        confirmStatus()
    }

    private fun confirmStatus() {
        if (getMethod().isAdminActive) {
            if (btClick != null) {
                btClick.visibility = View.GONE
                btClose.visibility = View.VISIBLE
            }
        } else {
            if (btClick != null) {
                btClick.visibility = View.VISIBLE
                btClose.visibility = View.GONE
            }
        }
    }


    private fun getMethod(): DeviceMethod {
        if (deviceMethod == null) {
            deviceMethod = DeviceMethod(applicationContext, this)
        }
        return deviceMethod as DeviceMethod
    }


    override fun onError(errorMessage: String) {
        alert(errorMessage) {
            yesButton { toast("确定") }
        }.show()
    }

}

